package cn.icanci.loopstack.ddk.web.controller.config;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.ddk.admin.biz.event.log.LogEvent;
import cn.icanci.loopstack.ddk.admin.biz.service.NoticeConfigService;
import cn.icanci.loopstack.ddk.common.enums.LogOperatorTypeEnum;
import cn.icanci.loopstack.ddk.common.enums.ModuleTypeEnum;
import cn.icanci.loopstack.ddk.common.model.PageList;
import cn.icanci.loopstack.ddk.common.model.Paginator;
import cn.icanci.loopstack.ddk.common.model.config.NoticeConfigVO;
import cn.icanci.loopstack.ddk.common.result.R;
import cn.icanci.loopstack.ddk.web.form.NoticeConfigQueryForm;
import cn.icanci.loopstack.ddk.web.mapping.NoticeConfigWebMapping;
import cn.icanci.loopstack.ddk.web.model.NoticeConfig;
import cn.icanci.loopstack.ddk.web.model.NoticeConfigListModel;
import cn.icanci.loopstack.lsi.event.EventDispatcher;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.google.common.collect.Lists;

/**
 * @author icanci
 * @since 1.0 Created in 2023/01/06 21:11
 */
@RestController
@RequestMapping("/ddk/noticeConfig")
public class NoticeConfigController {
    @Resource
    private NoticeConfigService    noticeConfigService;
    @Resource
    private NoticeConfigWebMapping noticeConfigWebMapping;
    @Resource
    private EventDispatcher        eventDispatcher;

    @PostMapping("query")
    public R query(@RequestBody NoticeConfigQueryForm form) {
        PageList<List<NoticeConfigVO>> ret = noticeConfigService.queryPage(noticeConfigWebMapping.web2vo(form.getNoticeConfig()), form.getPaginator().getCurrentPage(),
            form.getPaginator().getPageSize());
        Paginator paginator = ret.getPaginator();
        Collection<List<NoticeConfigVO>> datas = ret.getData();
        List<NoticeConfigListModel> resp = Lists.newArrayList();
        if (CollectionUtils.isEmpty(datas)) {
            return R.builderOk().data("queryPage", new PageList<>(resp, paginator)).build();
        }
        for (List<NoticeConfigVO> data : datas) {
            resp.add(buildResp(data));
        }
        return R.builderOk().data("queryPage", new PageList<>(resp, paginator)).build();
    }

    /**
     * 此处设计的比较尴尬
     * 
     * @param data data
     * @return 返回数据
     */
    private NoticeConfigListModel buildResp(List<NoticeConfigVO> data) {
        NoticeConfigListModel config = new NoticeConfigListModel();
        NoticeConfigVO noticeConfig = data.iterator().next();
        config.setId(noticeConfig.getId());
        config.setUuid(noticeConfig.getUuid());
        config.setDesc(noticeConfig.getDesc());
        config.setCreateTime(noticeConfig.getCreateTime());
        config.setUpdateTime(noticeConfig.getUpdateTime());
        config.setIsDelete(noticeConfig.getIsDelete());
        config.setEnv(noticeConfig.getEnv());
        config.setNoticeConfigName(noticeConfig.getNoticeConfigName());
        config.setNoticeConfigResource(noticeConfig.getNoticeConfigResource());
        config.setNoticeValue(noticeConfig.getNoticeValue());
        config.setAppUuid(data.stream().map(NoticeConfigVO::getAppUuid).collect(Collectors.toList()));
        return config;
    }

    @PostMapping("save")
    public R save(@RequestBody NoticeConfigListModel noticeConfig) {
        List<NoticeConfig> request = Lists.newArrayList();

        if (CollectionUtils.isNotEmpty(noticeConfig.getAppUuid())) {
            for (String uuid : noticeConfig.getAppUuid()) {
                request.add(mapper2NoticeConfig(noticeConfig, uuid));
            }
        }

        noticeConfigService.save(noticeConfigWebMapping.webs2vos(request), noticeConfig.getAppUuid());
        eventDispatcher.fire(new LogEvent(noticeConfig.getNoticeConfigResource(), ModuleTypeEnum.DDK_NOTICE_CONFIG, JSONUtil.toJsonStr(noticeConfig), LogOperatorTypeEnum.UPDATE),
            false);
        return R.builderOk().build();
    }

    private NoticeConfig mapper2NoticeConfig(NoticeConfigListModel noticeConfig, String uuid) {
        NoticeConfig config = new NoticeConfig();
        config.setId(noticeConfig.getId());
        config.setUuid(noticeConfig.getUuid());
        config.setDesc(noticeConfig.getDesc());
        config.setCreateTime(noticeConfig.getCreateTime());
        config.setUpdateTime(noticeConfig.getUpdateTime());
        config.setIsDelete(noticeConfig.getIsDelete());
        config.setEnv(noticeConfig.getEnv());
        config.setNoticeConfigName(noticeConfig.getNoticeConfigName());
        config.setNoticeConfigResource(noticeConfig.getNoticeConfigResource());
        config.setNoticeValue(noticeConfig.getNoticeValue());
        config.setAppUuid(uuid);
        return config;
    }

    @GetMapping("validateNoticeConfigName/{noticeConfigName:.*}")
    public R validateNoticeConfigName(@PathVariable("noticeConfigName") String noticeConfigName) {
        return R.builderOk().data("result", CollectionUtils.isEmpty(noticeConfigService.queryByNoticeConfigName(noticeConfigName))).build();
    }

    @GetMapping("validateNoticeConfigSource/{noticeConfigResource:.*}")
    public R validateNoticeConfigSource(@PathVariable("noticeConfigResource") String noticeConfigResource) {
        return R.builderOk().data("result", CollectionUtils.isEmpty(noticeConfigService.queryByNoticeConfigResource(noticeConfigResource))).build();
    }

}
